<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/7/10 0010
 * Time: 17:12
 */

namespace App\FreeBird\Models;

use App\Admin\Contracts\MemberLevelInterface;
use App\Admin\Models\UcenterMemberModel;
use App\Base\Models\BaseModel;
use App\FreeBird\Contracts\InfoClassInterface;
use App\FreeBird\Contracts\RebateLogInterface;
use App\Weixin\Models\WeixinUserModel;
use Illuminate\Database\Eloquent\SoftDeletes;

class InfoModel extends BaseModel
{
    use SoftDeletes;
    /**
     * 表名
     * @var string
     */
    public $table = "freebird_info";
    /**
     * 时间自动
     * @var bool
     */
    public $timestamps = true;
    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'create_time';  //修改添加时间字段
    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'update_time';

    /**
     * 删除
     */
    const DELETED_AT = 'delete_time';
    /**
     * 模型的日期字段保存格式。
     */
    protected $dateFormat = 'U';

    /**
     * 拼上用户名与密码
     * @var array
     */
    protected $appends = ["busername","order_status_name","user_header","b_user_header"];


    /**
     * 行业分类
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function infoClass(){
        return $this->hasOne(InfoClassModel::class,"id","class");
    }


    /**
     * 下订单者
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function makeOrderUser(){
        return $this->hasOne(WeixinUserModel::class,"uid","uid");
    }

    /**
     * 完成订单
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function filshOrderUser(){
        return $this->hasOne(WeixinUserModel::class,"uid","buid");
    }

    /**
     * 微信用户模块
     * @return WeixinUserModel
     */
    public function getWeixinUserModel(){
        return app()->make(WeixinUserModel::class);
    }
    /**
     * 获取用户模块
     * @return UcenterMemberModel
     */
    public function getUcenterMemberModel(){
        return app()->make(UcenterMemberModel::class);
    }

    /**
     * 用户头像
     */
    public function getUserHeaderAttribute(){
        $uid=$this->uid;
       return $this->getWeixinUserModel()->newQuery()->where("uid",$uid)->value("headimgurl");
    }
    /**
     * 用户头像
     */
    public function getBUserHeaderAttribute(){
        $buid=$this->buid;
        return $this->getWeixinUserModel()->newQuery()->where("uid",$buid)->value("headimgurl");
    }
    /**
     * 订单状态名称
     * @return string
     */
    public function getOrderStatusNameAttribute(){
        $orderStatus=$this->order_status;
        return $this->orderStatusNameArr($orderStatus);
    }

    public function orderStatusNameArr($type){
         switch ($type){
             case  '10':
                  $returnName='未接单';
                  break;
             case  '20':
                 $returnName='已支付订单';
                 break;
             case  '30':
                 $returnName='已接单';
                 break;
             case  '40':
                 $returnName='待完成';
                 break;
             case  '50':
                 $returnName='已完成';
                 break;
             case  '0':
                 $returnName='取消';
                 break;
             case  '35':
                 $returnName='退单';
                 break;
             case  '36':
                 $returnName='放弃订单';
                 break;
             case  '5':
                 $returnName='投诉状态';
                 break;
             case  '45':
                 $returnName='订单关闭';
                 break;
         }
         return $returnName;
    }
    /**
     * 数组
     * @var array
     */
    public $rarr=array();
    /**
     * 得到父级数组
     * @param int
     * @return array
     */
    public function get_parent($myid,$refer,$pidname='pid')
    {
        if (!isset($refer[$myid])){
            return false;
        }
        if($refer[$myid][$pidname]==0){
            return true;
        }else{
            $this->rarr[]=$refer[$refer[$myid][$pidname]];  //父类值
            $this->get_parent($refer[$myid][$pidname],$refer);
        }
    }
    /**
     * 获取行业分类
     * @return InfoClassInterface
     */
    public function getInfoClassInterface(){
        return app()->make(InfoClassInterface::class);
    }

    /**
     * 获取分类属性
     * @param $value
     */
    public function setClassAttribute($value){
        //获取分类
        $classList=$this->getInfoClassInterface()->getClassAll([]);
        $classArr=$classList->toArray();

        $newArr=array();
        foreach ($classArr as $key=>$val){
            $newArr[$val['id']]=$val;
        }
        $this->rarr[]=$newArr[$value];
        $this->get_parent($value,$newArr);
        if(!empty($this->rarr)){
            $makeArr=array();

            foreach ($this->rarr as $sv)
            {
                $makeArr[]=$sv['name'];
            }
            $this->attributes['class_name'] = "|".implode("|",$makeArr)."|";
        }
        $this->attributes['class'] = $value;

    }

    /**
     * 填的数量时总价是数量
     * @param $value
     */
    public function setNumAttribute($value){
        $amount=$this->attributes["amount"];
        $this->attributes['all_amount'] = number_format($amount*$value,2);
        //生成订单号
       // $paySn=$this->makePaySn(AUID);
       // $orderSn=$this->makeOrderSn($paySn);
        //$this->attributes["order_sn"]=$orderSn;  //订单号
        //$this->attributes["pay_sn"]=$paySn;  //支付号
        $this->attributes["num"]=$value;  //支付号
    }

    /**
     * 是时转化
     * @param $value
     */
    public function setStartTimeAttribute($value)
    {
        $this->attributes["start_time"]=strtotime($value);
    }


    /**
     * 名字
     * @return string
     */
    public function getBusernameAttribute(){
        $uid=$this->buid;
        $info=$this->getWeixinUserModel()->newQuery()->where("uid",$uid)->first();
        return $info["truename"] ?? '无';
    }

    /**
     * 返佣日志表
     * @return RebateLogInterface
     */
    public function getRebateLogInterface(){
        return app()->make(RebateLogInterface::class);
    }
    /**
     * 订单完成后计算给用户
     */
    public function setUserBalance($info){
        $userInfo = $this->getWeixinUserModel()->newQuery()->where("uid", $info->buid)->first();  //用户是否已是Vip如果是则加上vip
        //直推计算
        if (!empty($userInfo->p1)) {
            $p1Data = array();
            $p1Data["order_sn"] = $info->order_sn;
            $p1Data["uid"] = $info->uid;
            $p1Data['mount_type']=1;
            $p1Data["all_mount"] =$info->all_amount;
            $bMount=$info->all_amount*get_config_param_value("TS_PUSH_DIECT_BACK")*get_config_param_value("TS_PUSH_BONUS_BACK")/10000;//在剩赏佣比例
            $p1Data["b_mount"] =number_format($bMount,2) ;
            $p1Data["source_type"] = 1;
            $p1Data["buid"] = $userInfo->p1;
            $p1Data['type'] = 2;
            if(!empty($bMount)){
                $this->getRebateLogInterface()->saveVipData($p1Data);
            }
        }
        //间推
        if (!empty($userInfo->p2)) {
            $p1Data = array();
            $p1Data["order_sn"] = $info->order_sn;
            $p1Data["uid"] = $info->uid;
            $p1Data['mount_type']=1;
            $p1Data["all_mount"] =$info->all_amount;
            $bMount= $info->all_amount*get_config_param_value("TS_PUSH_PART_BACK")*get_config_param_value("TS_PUSH_BONUS_BACK")/10000;  //在剩赏佣比例
            $p1Data["b_mount"] =number_format($bMount,2) ;
            $p1Data["source_type"] = 2;
            $p1Data["buid"] = $userInfo->p2;
            $p1Data['type'] = 2;
            if(!empty($bMount)){
                $this->getRebateLogInterface()->saveVipData($p1Data);
            }
        }
        //赏金
        $p3Data = array();
        $p3Data["order_sn"] = $info->order_sn;
        $p3Data["uid"] = $info->uid;
        $p3Data['mount_type']=1;
        $p3Data["all_mount"] =$info->all_amount;
        $bMount= $info->all_amount*(100-get_config_param_value("TS_PUSH_BONUS_BACK"))/100;  //在剩赏佣比例
        $p3Data["b_mount"] =number_format($bMount,2) ;
        $p3Data["source_type"] = 3;  //赏金
        $p3Data["buid"] = $info->buid;  //订单得佣者
        $p3Data['type'] = 2;
        return   $this->getRebateLogInterface()->saveVipData($p3Data);
    }
}